class Solution {
    public int pivotIndex(int[] nums) {
        int n = nums.length;
        int[] f = new int[n];
        int[] g = new int[n];
        for(int i = 1; i < n; i++) {
            f[i] = f[i-1] + nums[i-1];
        }
        for(int i = n - 2; i >= 0; i--) {
            g[i] = g[i+1] + nums[i+1];
        }
        int index = 0;
        while(index < n) {
            if(f[index] == g[index]) {
                return index;
            }
            index++;
        }
        return -1;
    }
}